<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/5
 * Time: 16:02
 */
namespace app\reserve\controller;

use think\Controller;
use think\Request;
use app\reserve\model\Onecardproduct as OneProductModel;

class Onecardproduct extends Controller{

    private $one;

    public function __construct()
    {
        $this->one = new OneProductModel();
    }

    /**
     * Notes:  添加一卡通产品
     * Create Date: 2018/10/5
     * Create by :lijie
     */
    public function addOneProduct(Request $request)
    {
        $param  = $request->post();
        $vali   = $this->validate($param,"Onecardproduct.addOneProduct");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        //
        $res = $this->one->addOne($param);
        if($res){
            return apiResponse(200,'添加成功',$res);
        }else {
            return apiResponse(201,"添加失败",$res);
        }
    }

    /**
     * Notes:  删除一卡通产品
     * Create Date: 2018/10/5
     * Create by :lijie
     */
    public function delOneProduct()
    {
        $id = input('post.id');
        if(empty($id)){
            return apiResponse(201,'id不能为空','');
        }
        $res = $this->one->delOne($id);
        if($res){
            return apiResponse(200,'删除成功',$res);
        }else {
            return apiResponse(201,"删除失败",$res);
        }
    }

    /**
     * Notes:  编辑一卡通产品
     * Create Date: 2018/10/5
     * Create by :lijie
     */
    public function editOneProduct(Request $request)
    {
        $param  = $request->post();
        $vali   = $this->validate($param,"Onecardproduct.editOneProduct");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        //
        $where['id']    = $param['id'];
        $where['status'] = array("NEQ",9);
        $res = $this->one->editOne($param,$where);
        if($res){
            return apiResponse(200,'编辑成功',$res);
        }else {
            return apiResponse(201,"编辑失败",$res);
        }
    }

    /**
     * Notes:  一卡通列表产品
     * Create Date: 2018/10/5
     * Create by :lijie
     */
    public function selectOneProduct(Request $request)
    {
        $param  = $request->post();
        $vali   = $this->validate($param,"Onecardproduct.selectOneProduct");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        // 商品名称筛选
        if(!empty($param['name'])){
            $where['name']  = array("LIKE",'%'.$param['name'].'%');
        }
        // 时间筛选
        if(!empty($param['start']) && !empty($param['end'])){
            $start  = strtotime($param['start']." 00:00:00");
            $end    = strtotime($param['end']  ." 23:59:59");
            $where['ctime'] = array(array("EGT",$start),array("ELT",$end),'and');
        }
        // 状态筛选
        $where['status'] = array("NEQ",9);
        if(isset($param['status']) && $param['status']!=""){
            $where['status'] = $param['status'];
        }
        //
        $res['list'] = $this->one->selectOne($where,$param['pagenum'],$param['listNum']);
        $res['total'] = $this->one->where($where)->count();
        if($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,"获取失败",$res);
        }
    }

    /**
     * Notes:  一卡通详情产品
     * Create Date: 2018/10/5
     * Create by :lijie
     */
    public function findOneProduct()
    {
        $id = input('post.id');
        if(empty($id)){
            return apiResponse(201,'id不能为空','');
        }
        $where['id']    = $id;
        $where['status'] = array("NEQ",9);
        $res = $this->one->findOne($where);
        if($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,"获取失败",$res);
        }
    }
}